//
//  ViewController.m
//  获取屏幕截图
//
//  Created by 伍银荣 on 2021/7/3.
//  Copyright © 2021 com.screen capture. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"----touchesBegan----");
    //开启图片类型的图形上下文
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //获取屏幕截图 (把view的截图放到图形上下文中,然后渲染)
    [self.view.layer renderInContext:ctx];
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片类型的图形上下文
    UIGraphicsEndImageContext();
    
    //保存到相册
    UIImageWriteToSavedPhotosAlbum(image, NULL, NULL, NULL);
    
    
}


@end
